﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Tauren.Form.DLL.Field_FormDLL;
using Tauren.Form.DLL.Resouces;
using Tauren.Form.Entity.Config;
using Tauren.Form.Entity.Const;
using Tauren.Form.Entity.Enum;
using Tauren.Form.Entity.Global;
using Tauren.Form.Entity.Input;
using Tauren.Form.Entity.Model;
using Tauren.Form.Entity.Output;
using Tauren.Form.Infrastructure.Common;
using Tauren.Form.Infrastructure.Globals;

namespace Tauren.Form.Service.Field_FormBLL
{
    /// <summary>
    /// 表单设置
    /// </summary>
    internal class V2Field_FormService : Field_FromImpService
    {
        private IField_FormBusiness business;
        private LongCommon longCommon;
        public V2Field_FormService(IField_FormBusiness _business, IConnectionBLLBase connectionBLL, IMenuResourcesBusiness resourcesBusiness) : base(_business, connectionBLL, resourcesBusiness)
        {
            this.business = _business;
            this.ResourcesInfo = this.GetResources(GlobalConst.RESOURCES_FLAG_MODULE_FIELD_FORM_KEY);
            this.JudePrmission(GlobalConst.RESOURCES_FLAG_MODULE_FIELD_FORM_KEY.ToLower());
            longCommon = new LongCommon();
        }

        public override double VersionName => 2.0;
        public override IResponseMessage Pages(QueryModel queryModel)
        {
            if (!HavePermission) return ErrorType.BUSINESS_WITHOUT_PERMISSION.Failure(this.CommonText(ErrorType.BUSINESS_WITHOUT_PERMISSION));
            ///获取基础数据库中表信息
            List<TableInfo> tableInfos = business.GetAll<TableInfo>();
            TableListModel<PageField_FormTable> tableListModel = new TableListModel<PageField_FormTable>();
            List<TableListHeaderModele> Headers = new List<TableListHeaderModele>();
            Headers.Add(new TableListHeaderModele() { Key = "rowNo", Name = CommonText("rowNo"), Width = 50, Align = "center", Fixed = true });
            Headers.Add(new TableListHeaderModele() { Key = "autoId", Name = CommonText("autoid"), Width = 70, Hide = true, Type = "primary_key", Align = "center", SorTable = SortType.custom.ToString(), Fixed = true });
            Headers.Add(new TableListHeaderModele() { Key = "cnName", Name = CommonText("thcnname"), Align = "left", Width = 220, IsOperation = true, Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "enName", Name = CommonText("thenname"), Align = "left", Width = 220, Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "currentVersion", Name = Resources("currentversion"), Width = 160, Align = "left", Fixed = true, SorTable = SortType.custom.ToString(), Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "dic_Status", Name = CommonText("thstate"), IsTag = true, Width = 100, Align = "center", SorTable = SortType.custom.ToString() });
            Headers.Add(new TableListHeaderModele() { Key = "dic_Creator", Name = CommonText("diccreator"), Width = 120, Align = "center" });
            Headers.Add(new TableListHeaderModele() { Key = "dic_Creatdate", Name = CommonText("diccreatdate"), Width = 160, Align = "center" });
            Headers.Add(new TableListHeaderModele() { Key = "action", Name = "操作", Width = 180, Align = "center" });
            long Total = 0;
            List<PageField_FormTable> Data = business.Pages(queryModel, out Total);

            if (Data.Any())
            {
                Data.ForEach(o =>
                {
                    o.Row_Style = ((FormStatus)o.Status) != FormStatus.delete && ((FormStatus)o.Status) != FormStatus.unloaded ? "" : "danger";
                    o.Dic_Creator = EmpDictionary.ContainsKey(o.Creator) ? EmpDictionary[o.Creator] : "";
                    o.Dic_Creatdate = o.Creatdate.Value.ToString(GlobalConst.DATE_FORMAT_SECONDS);
                    o.Dic_Status = this.Resources($"formstatus{((FormStatus)o.Status) + ""}");
                    o.Operations = new List<SystemButton>();
                    if (Global.IsAdmin && (Buttons is null || !Buttons.Any()))
                    {
                        BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.edit);
                        BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.set);
                        BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.detaile);
                        if (o.Status == (int)FormStatus.unloaded || o.Status == (int)FormStatus.design)
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.delete);
                        if (o.Status == (int)FormStatus.installed)
                        {
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.unloaded);
                        }
                        if (o.Status == (int)FormStatus.unloaded)
                        {
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.enable);
                        }
                    }
                    else
                    {
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.edit.ToString()))
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.edit);
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.set.ToString()))
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.set);
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.detaile.ToString()))
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.detaile);
                        if (Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.delete.ToString()) && (o.Status == (int)FormStatus.unloaded || o.Status == (int)FormStatus.design))
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.delete);
                        if (o.Status == (int)FormStatus.installed && Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.unloaded.ToString()))
                        {
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.unloaded);
                        }
                        if (o.Status == (int)FormStatus.unloaded && Buttons.Any(o => o.Location == ButtomLocation.center.ToString() && o.Value == ButtomType.unloaded.ToString()))
                        {
                            BuliderButtons(o.Operations, o.CurrentVersion + "", o.AutoId, ButtomType.enable);
                        }
                    }
                });
            }
            tableListModel.Body = Data;
            tableListModel.Total = Total;
            tableListModel.Header = Headers;
            if (Global.IsAdmin && (Buttons is null || !Buttons.Any()))
            {
                BuliderButtons(tableListModel.Operations, "", 0, ButtomType.add);
            }
            else
            {
                if (Buttons.Any(o => o.Value == ButtomType.add.ToString()))
                    BuliderButtons(tableListModel.Operations, "", 0, ButtomType.add);
            }
            return tableListModel.Succeed();
        }
    }
}
